<include file="Public/header" title="{$siteSetting['site_seo_title']}" keywords="{$siteSetting['site_seo_keywords']}" description="{$siteSetting['site_seo_description']}" />

	<!-- footer -->
    <footer class="x-footer this-page" style="display: none;">
		<ul class="x-row x-footer-bar">
			<li class="x-col-6-12">
				<a href="javascript:;" class="button button-block button-highlight button-large" id="reScan">重新扫码</a>
			</li>
			<li class="x-col-6-12">
				<a href="javascript:;" class="button button-block button-primary button-large" id="btnAjaxSubmit">确定借阅</a>
			</li>
		</ul>
	</footer>
	
	<!-- MAIN -->
    <section class="x-content x-m-b-50 this-page" style="display: none;">
		<!-- section -->
		<section class="x-box">
			<div class="x-box-body">
				<ul class="x-row x-list-input">
					<li class="x-col-12-12 with-border">
						<div class="text-center">
							<img alt="no pic" src="__TMPL__Public/assets/img/nopic128.png" id="thumb" style="height: 200px;" />
						</div>
					</li>
					<li class="x-col-12-12 with-border">
						<div class="x-row">
							<div class="x-col-3-12 x-label-name">ISBN</div>
							<div class="x-col-9-12 x-label-value" id="isbn"></div>
						</div>
					</li>
					<li class="x-col-12-12 with-border">
						<div class="x-row">
							<div class="x-col-3-12 x-label-name">书名</div>
							<div class="x-col-9-12 x-label-value" id="name"></div>
						</div>
					</li>
					<li class="x-col-12-12 with-border">
						<div class="x-row">
							<div class="x-col-3-12 x-label-name">作者</div>
							<div class="x-col-9-12 x-label-value" id="author"></div>
						</div>
					</li>
					<li class="x-col-12-12 with-border">
						<div class="x-row">
							<div class="x-col-3-12 x-label-name">译者</div>
							<div class="x-col-9-12 x-label-value" id="translator"></div>
						</div>
					</li>
				</ul>
			</div>
				
			<form id="form">
			<input type="hidden" name="card_id" value="{$customer.card_id}" />
			<input type="hidden" name="code" id="code" />
			</form>
		</section>
    </section>
	
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

<script>
$(function () {
    // 通过config接口注入权限验证配置
    wx.config({
	    debug: false, 							// 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
		appId: '{$signPackage.appId}',			// 必填，公众号的唯一标识
		timestamp: '{$signPackage.timestamp}',	// 必填，生成签名的时间戳
		nonceStr: '{$signPackage.nonceStr}',	// 必填，生成签名的随机串
		signature: '{$signPackage.signature}',	// 必填，签名，见附录1
		jsApiList: ['scanQRCode']		// 必填，需要使用的JS接口列表，所有JS接口列表见附录2
	});

    // 通过ready接口处理成功验证，调起微信扫一扫接口扫描二维码并返回结果
	var xloading = xpopup.loading({content:'扫码系统初始化中...'});
    wx.ready(function() {
    	xloading.destroy();
    	$(".this-page").show();
    	scanFunc();
    });
    
    wx.error(function(res) {
    	console.log(res);
    });

	$("#btnAjaxSubmit").click(function() {
		// validate
		if ($("#code").val() == '') {
           	xpop({
               	title: '获取图书信息失败',
                msg: '请确扫描图书二维码！',
                buttons: [{
                    text: '确定',
                    'class': 'weui_btn_dialog primary',
                    handler: function ($this) {
                		$this.destroy();
                    }
                }]
            });
    		return false;
		}
	    
	    // submit
		$.ajax({
			type: 'post',
			url: "{:U('Borrow/Index/save')}",
	        contentType: 'application/x-www-form-urlencoded',
			data: $("#form").serialize(),
			dataType: 'json',
			async: false,
			success: function(data) {
				//	console.log(data);
				if (data.code == 0) {
					xpop.toast({
					    type: 'success',
				        msg: data.msg,
				        delay: 1000,
				        afterClose: function() {
				        	window.location = "{:U('Borrow/Index/getCustBorrowList')}";
				        }
				    });
				} else {
		           	xpop({
		               	title: '借阅失败',
		                msg: data.msg,
		                buttons: [{
		                    text: '确定',
		                    'class': 'weui_btn_dialog primary',
		                    handler: function ($this) {
		                		$this.destroy();
		                    }
		                }]
		            });
				}
			},
            error: function (XMLHttpRequest, textStatus, errorThrown) {
				//
            }
		});
	    
		return false;
	});
	
	/* re scan */
	$("#reScan").click(function() {
		scanFunc();
    });

});

function scanFunc() {
	// clear
	$("#code").val("");
	$("#isbn").text("");
	$("#name").text("");
	$("#author").text("");
	$("#translator").text("");
	
	$("#thumb").attr('src', '__TMPL__Public/assets/img/nopic128.png');

	/* 扫描二维码 */
	wx.scanQRCode({
        needResult: 1,
        scanType: ["qrCode","barCode"],
        success: function (res) {
			$.ajax({
				type: "get",
	            contentType: "application/json; charset=utf-8",
	            url: "{:U('Book/Index/findByCode')}?code=" + encodeURIComponent(res.resultStr),
	            dataType: "json",												// 一定要确实正确
	            success: function (data) {
	        		if (data.code == 0) {
		    			var book = data.obj;

		            	$("#code").val(encodeURIComponent(res.resultStr));
		            	$("#isbn").text(book.isbn);
		            	$("#name").text(book.name);
		            	$("#author").text(book.author);
		            	$("#translator").text(book.translator);
		            	
		            	$("#thumb").attr('src', book.thumb);
	        		} else {
			           	xpop({
			               	title: '获取图书信息失败',
			                msg: data.msg,
			                buttons: [{
			                    text: '确定',
			                    'class': 'weui_btn_dialog primary',
			                    handler: function ($this) {
			                		$this.destroy();
			                    }
			                }]
			            });
	        		}
	            },
	            error: function (XMLHttpRequest, textStatus, errorThrown) {
		           	xpop({
		               	title: '获取图书信息失败',
		                msg: '请确扫描图书二维码！',
		                buttons: [{
		                    text: '确定',
		                    'class': 'weui_btn_dialog primary',
		                    handler: function ($this) {
		                		$this.destroy();
		                    }
		                }]
		            });
	            }
	        });
        }
	});
}
</script>
</body>
</html>